package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.db.soul.SoulReport;
import com.itheima.mapper.SoulReportMapper;
import com.itheima.service.db.SoulReportService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class SoulReportServiceImpl implements SoulReportService {

    @Autowired
    SoulReportMapper soulReportMapper;


    @Override
    public void saveOrUpdateScore(Long userId, int score, Long soulExamId) {
        QueryWrapper<SoulReport> qw = new QueryWrapper<>();
        qw.eq("user_id", userId);
        qw.eq("soul_exam_id", soulExamId);

        SoulReport soulReport = soulReportMapper.selectOne(qw);

        if (soulReport == null) {
            soulReport = new SoulReport();
            soulReport.setUserId(userId);
            soulReport.setScore(score);
            soulReport.setSoulExamId(soulExamId);

            soulReportMapper.insert(soulReport);
        } else {
            soulReport.setScore(score);

            soulReportMapper.updateById(soulReport);
        }
    }

    @Override
    public SoulReport findOne(Long userId, Long soulExamId) {
        QueryWrapper<SoulReport> qw = new QueryWrapper<>();
        qw.eq("user_id", userId);
        qw.eq("soul_exam_id", soulExamId);

        return soulReportMapper.selectOne(qw);

    }
}
